"""
Sphinx plugins for Chinese documentation.
"""

from string import printable

def setup(app):
    app.connect('source-read', merging_lines)

def merging_lines(app, docname, source):
    for x in range(len(source)):
        lines = source[x].split('\n')

        text = ''
        needmerge = False
        for line in lines:
            sl = line.lstrip()

            if needmerge:
                if len(sl) == 0 or sl[0] in printable:
                    text += '\n'
                    needmerge = False

            if needmerge:
                line = sl
                needmerge = False

            text += line

            if not text[-1] in printable:
                needmerge = True
            else:
                text += '\n'

        source[x] = text
